import 'babel-polyfill';
import axios from 'axios';
import qs from 'qs';
  
axios.defaults.timeout = 5000;
axios.defaults.withCredentials = true;  //默认跨越
axios.defaults.headers['Content-Type'] = 'application/json';

axios.defaults.headers['X-Requested-With'] = 'XMLHttpRequest';

axios.defaults.baseURL = 'http://10.0.66.113:8080';
//axios.defaults.baseURL = 'http://10.67.29.164:8084';
//axios.defaults.baseURL = 'http://10.67.23.50:8084';

axios.interceptors.request.use((config) => {
		if (sessionStorage.getItem("token")) {
        config.headers.Authorization = `${sessionStorage.getItem("token")}`;
    }
    return config;
},(error) =>{
    return Promise.reject(error);
});

axios.interceptors.response.use((res) =>{
    if(res.status != '200'){
    		return Promise.reject(res);
    }
    return  Promise.resolve(res.data);;
}, (error) => {
    console.log("network error");
    return Promise.reject(error);
});

const http = {
	
	get: (url,params,callback=null)=>{
		let options = {method:'get',url,withCredentials:true};
		if(callback){ 
			options.params = params;
		}else{
			callback = params;
		}
		axios(options).then((redata) => {
			callback(redata);
		},(err)=>{
			callback({err,success:false});
		})
	},
	
	post: (url,data,params,callback=null)=>{
		let options = {method:'post', url, data};
		if(callback){ 
			options.params = params;
		}else{
			callback = params;
		}
		axios(options).then((redata) => {
			callback(redata);
		},(err)=>{
			callback({err,success:false});
		})
	},
	
	delete: (url,callback)=>{
		axios({ method:'delete', url}).then((redata) => {
			callback(redata);
		},(err)=>{
			callback({err,success:false});
		})
	},
	
	put: (url,data,callback)=>{
		axios({ method:'put', url, data }).then((redata) => {
			callback(redata);
		},(err)=>{
			callback({err,success:false});
		})
	}
	
}

export default http;